package com.zs.propertymanage.controller;

import com.zs.propertymanage.annotation.SystemLog;
import com.zs.propertymanage.dataObj.R;
import com.zs.propertymanage.dataObj.entity.Tenant;
import com.zs.propertymanage.dataObj.param.CreateWorkOrderParams;
import com.zs.propertymanage.service.impl.TenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/tenant")
@CrossOrigin
public class TenantController {

    @Autowired
    private TenantService tenantService;

    /**
     * 获取未经处理的租户信息列表
     *
     * @return R
     */
    @GetMapping("/plain")
    @ResponseBody
    public R getAllTenantWithPlain() {
        return tenantService.getAllTenantWithPlain();
    }

    /**
     * 根据条件获取的租户信息列表
     *
     * @param search      根据名称模糊查询参数
     * @param currentPage 当前页
     * @param pageSize    每页大小
     * @return R
     */
    @GetMapping("")
    @ResponseBody
    public R getAllTenantWithCondition(String search, Integer currentPage, Integer pageSize) {
        return tenantService.getAllTenantWithCondition(search, currentPage, pageSize);
    }

    /**
     * 添加租户信息
     *
     * @param tenant 租户信息
     * @return R
     */
    @PostMapping("")
    @ResponseBody
    @SystemLog(name = "添加租户")
    public R addTenant(@RequestBody Tenant tenant) {
        return tenantService.addTenant(tenant);
    }

    /**
     * 更新租户信息
     *
     * @param tenant 租户信息
     * @return R
     */
    @PutMapping("")
    @ResponseBody
    @SystemLog(name = "更新租户")
    public R updateTenant(@RequestBody Tenant tenant) {
        return tenantService.updateTenant(tenant);
    }

    /**
     * 删除租户信息
     *
     * @param id 租户id
     * @return R
     */
    @DeleteMapping("/{id}")
    @ResponseBody
    @SystemLog(name = "删除租户")
    public R deleteTenantById(@PathVariable String id) {
        return tenantService.deleteTenantById(id);
    }

    // 租户端数据统计概览
    @GetMapping("/stat/{tenantId}")
    @ResponseBody
    public R getTenantStat(@PathVariable String tenantId) {
        return tenantService.getTenantStat(tenantId);
    }

    // 查询当前租户下的房屋
    @GetMapping("/house/{id}")
    @ResponseBody
    public R getHouseByTenantId(@PathVariable String id, String search, Integer currentPage, Integer pageSize) {
        return tenantService.getHouseByTenantId(id, search, currentPage, pageSize);
    }

    // 查询可租用房屋信息
    @GetMapping("/rentable")
    @ResponseBody
    public R getRentableHouse(String search, Integer currentPage, Integer pageSize) {
        return tenantService.getRentableHouse(search, currentPage, pageSize);
    }

    // 新增租屋
    @PostMapping("/rent")
    @ResponseBody
    @SystemLog(name = "添加租屋")
    public R rentHouse(@RequestBody Map<String, String> params) {
        String tenantId = params.get("tenantId");
        String houseId = params.get("houseId");
        return tenantService.rentHouse(tenantId, houseId);
    }

    // 停止租房
    @PostMapping("/stopRent")
    @ResponseBody
    @SystemLog(name = "停止租房")
    public R stopRent(@RequestBody Map<String, String> params) {
        String houseId = params.get("houseId");
        return tenantService.stopRent(houseId);
    }

    // 调整房屋状态
    @PutMapping("/status")
    @ResponseBody
    @SystemLog(name = "修改房屋状态")
    public R updateHouseStatus(@RequestBody Map<String, String> params) {
        String houseId = params.get("houseId");
        Integer statusId = Integer.parseInt(params.get("statusId"));
        return tenantService.updateHouseStatus(houseId, statusId);
    }

    // 查询当前租户下的工单
    @GetMapping("/workOrder/{id}")
    @ResponseBody
    public R getWorkOrderByTenantId(@PathVariable String id, String search, Integer currentPage, Integer pageSize) {
        return tenantService.getWorkOrderByTenantId(id, search, currentPage, pageSize);
    }


    // 查询当前租户下的所有房屋列表
    @GetMapping("/allHouse/{tenantId}")
    @ResponseBody
    public R getAllHouseByTenantId(@PathVariable String tenantId) {
        return tenantService.getAllHouseByTenantId(tenantId);
    }

    // 新建工单
    @PostMapping("/createWorkOrder/{tenantId}")
    @ResponseBody
    @SystemLog(name = "新建工单")
    public R addWorkOrder(@PathVariable String tenantId, @RequestBody CreateWorkOrderParams createWorkOrder) {
        return tenantService.createWorkOrder(tenantId, createWorkOrder.getHouseId(), createWorkOrder.getWorkOrderCategoryId(), createWorkOrder.getOrderRemark());
    }

}
